<!-- 文件所有权归北京优博泰克 -->
<!-- 新增最小单元子任务 -->
<template>
    <dy-dialog :title="title" ref="dialog" @submitHandle="submitHandle" :loading="loading">
        <el-form ref="ruleForm" :model="formModel" label-width="130px" label-position="left">
            <el-form-item v-for="item in fields" :label="item.label" :required="item.required" :key="item.key">
                <el-input v-if="!item.type" v-model="formModel[item.key]"></el-input>
                <el-select v-else v-model="formModel[item.key]" placeholder="请选择">
                    <el-option v-for="it in item.option" :key="it.value" :label="it.label" :value="it.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
    </dy-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { useCach } from '@/store/cach'
import api from '@/api'

const pageCach = useCach()
const loading = ref(false)
const title = ref('')
const tab = ref('')
const formModel = ref({})

const emit = defineEmits(['gettabledata'])


const fields = ref([
    {
        label: '子任务',
        key: 'subtask',
        required: true
    },
    {
        label: '运行方法',
        key: 'runmethod',
        required: true
    },
    {
        label: '参考标准',
        key: 'reference',
        required: true
    },
    {
        label: '类型',
        key: 'elementtype',
        type: 'select',
        option: [
            {
                value: '外特性',
                label: '外特性'
            },
            {
                value: '万有特性',
                label: '万有特性'
            },
            {
                value: 'DOE运行工况',
                label: 'DOE运行工况'
            },
            {
                value: 'WHSC',
                label: 'WHSC'
            },
            {
                value: 'ESC',
                label: 'ESC'
            },
            {
                value: '其他',
                label: '其他'
            }
        ],
        required: true
    },
    {
        label: '备注',
        key: 'remark',
        required: false
    }
])

// 方法
const handleClose = () => {
    pageCach.changeShowModal(false)
    pageCach.changeModalStatus('')
    formModel.value = {}
}

const openFunction = (options) => {
    title.value = options.title
    tab.value = options.tab
    if (options.row) {
        formModel.value = JSON.parse(JSON.stringify(options.row))
    }
}

const submitHandle = async () => {
    const filter = fields.value.filter(it => it.required && !formModel.value[it.key])
    if (filter.length) {
        ElMessage({
            title: '请完善信息后再试!',
            type: 'warning'
        })
        return
    }

    // 发送请求添加数据
    loading.value = true
    const method = formModel.value.id ? 'patch' : 'post'
    formModel.value.tabletype = tab.value
    const res = await api.changeaxiosdata('testelement', method, formModel.value)
    if (res) {
        ElMessage({
            title: '操作成功!',
            type: 'success'
        })
        handleClose()
        emit('gettabledata')
    }
    loading.value = false
}

// 如果需要暴露方法给父组件
defineExpose({
    openFunction
})
</script>
<style scoped></style>